package cn.goodym.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.goodym.exception.CustomException;
import cn.goodym.po.CaseItem;
import cn.goodym.po.CasePort;
import cn.goodym.po.JsonVar;
import cn.goodym.po.PagingVO;
import cn.goodym.service.CaseItemService;
import cn.goodym.service.CasePortService;
import cn.goodym.service.EnvService;

import com.alibaba.fastjson.JSONArray;

/**
 * 接口管理控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/port")
public class PortController {

	@Resource
	private CasePortService casePortService;
	@Resource
	private EnvService envService;
	@Resource
	private CaseItemService caseItemService;

	/**
	 * 展示接口列表数据
	 * 
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showPort")
	public String showEnv(Model model, Integer page) throws Exception {

		List<CasePort> list = null;
		// // 页码对象
		PagingVO pagingVO = new PagingVO();
		// 设置总页数
		pagingVO.setTotalCount(casePortService.getCount());
		if (page == null || page == 0) {
			pagingVO.setToPageNo(1);
			list = casePortService.findByPaging(1);
		} else {
			pagingVO.setToPageNo(page);
			list = casePortService.findByPaging(page);
		}
		model.addAttribute("casePortList", list);
		model.addAttribute("pagingVO", pagingVO);
		return "port/showPort";
	}

	/**
	 * 跳转到添加接口页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addPort", method = { RequestMethod.GET })
	public String addPortUI(Model model) throws Exception {
		/*List<Env> envList = envService.findAll();
		model.addAttribute("envList", envList);*/
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		return "port/addPort";
	}

	/**
	 * 添加接口数据
	 * 
	 * @param casePort
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addPort", method = { RequestMethod.POST })
	public String addPort(CasePort casePort, Model model) throws Exception {

		Date date = new Date();
		casePort.setCreateTime(date);
		Boolean result = casePortService.save(casePort);

		if (!result) {
			model.addAttribute("message", "添加失败");
			return "error";
		}
		return "redirect:/port/showPort";
	}

	/**
	 * 删除接口数据
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/removePort")
	public String removeEnv(Integer id) throws Exception {
		if (id == null) {
			return "port/showPort";
		}
		casePortService.removeById(id);
		return "redirect:/port/showPort";
	}

	/**
	 * 跳转到编辑接口
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editPort", method = { RequestMethod.GET })
	public String editPortUI(Integer id, Model model) throws Exception {
		if (id == null) {
			return "redirect:/port/showPort";
		}
		CasePort casePort = casePortService.findById(id);
		
		List<JsonVar> listVar = new ArrayList<JsonVar>();  
		listVar = JSONArray.parseArray(casePort.getParamters(), JsonVar.class);//这里的t是Class<T> 
		model.addAttribute("listVar", listVar);
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);

		if (casePort == null) {
			throw new CustomException("未找到该id");
		}

		model.addAttribute("casePortList", casePort);
		return "port/editPort";
	}

	/**
	 * 搜索
	 * @param name
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectPort", method = { RequestMethod.POST })
	private String selectPort(String name, Model model) throws Exception {

		List<CasePort> casePortList = casePortService.findByName(name);

		model.addAttribute("casePortList", casePortList);
		return "port/showPort";
	}
	
	/**
	 * 修改接口数据
	 * @param casePort
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editPort", method = { RequestMethod.POST })
	public String editPort(CasePort casePort) throws Exception {

		CasePort CasePorts = casePortService.findById(casePort.getId());
		casePort.setCreateTime(CasePorts.getCreateTime());
		Date date = new Date();
		casePort.setUpdateTime(date);
		casePort.setPortName(casePort.getPortName());
		
		String cp = casePort.getParamters().replace("\\t", "");
		String cp3 = cp.replace("\\n", "");
		casePort.setParamters(cp3);
		
		casePortService.updateById(casePort);

		return "redirect:/port/showPort";
	}

}
